package net.optix.ip.commands
{
	import com.adobe.cairngorm.business.Responder;
	import com.adobe.cairngorm.commands.Command;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	
	import mx.controls.Alert;
	
	import net.optix.ip.business.*;
	import net.optix.ip.events.GetInventionEvent;
	import net.optix.ip.events.UpdateInventionEvent;
	import net.optix.ip.model.ModelLocator;

	public class UpdateInventionCommand implements Command, Responder
	{
		// attributes ============================
		private var model : ModelLocator = ModelLocator.getInstance();

		private var bOption:int			=	0;
	
		// functions ============================
		public function execute( event:CairngormEvent ) : void
		{
			var myEvent:UpdateInventionEvent = UpdateInventionEvent( event );
			var delegate:InventionDelegate = new InventionDelegate();     
			delegate.updateInvention( this , myEvent.vo );
			

		}
		
		//----------------------------------------------------------------------------
		public function onResult( event : * = null ) : void
		{
			var title:String	=	"Result of update invention ";
			var message:String;
			
			try
			{
				var	result:int				=	event.result.result;
				switch(result)
				{
					case -2:
						message	=	"Access Denied .";
						break;
					case -1:
						message	=	"The invention can't be editing now , may it is already be published !";
						break;
					case 0:
						message	=	"Program error!";
						break;
					case 1:
						message	=	"You have successfully updated the invention !";
						model.close		=	"no";
						model.close		=	"yes";
						
						var getInvention:GetInventionEvent	=	
							new GetInventionEvent(model.currentInvention.inventionID);
						com.adobe.cairngorm.control.CairngormEventDispatcher.getInstance().dispatchEvent(getInvention);
						
						break;
				}
			}
			catch(error:Error)
			{
				message	=	"Program error";
			}
			
			model.bSubmit	=	true;
			
			
			if(this.bOption	!= 1)
			{
				var alert:Alert	=	 
					Alert.show(message,title,Alert.OK, null,null, null,  Alert.OK );
				alert.width		=	360;
				alert.height	=	130;
			}
		}
		
		//----------------------------------------------------------------------------
		public function onFault( event : * = null ) : void
		{
			model.bSubmit		=	true;
			var title:String	=	"Result of update invention 12";
			var message:String;
			message	=	"Program error";
			var alert:Alert	=	 
					Alert.show(message,title,Alert.OK, null,null, null,  Alert.OK );
			alert.width		=	360;
			alert.height	=	130;
		}
	}
}